package net.gd.utils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class ApplicationConf {
	private static String appFilePath="/home/ABD/application.properties";
//	private static String appFilePath="/Users/uriel/git/ABDCustomize1.0/ABDCustomize1.0/resource/application.properties";
	//加载的属性
	static Properties p = new Properties();
	//
	static Map<String,String[]> jobDepMap = new HashMap<String,String []>();
	//数据源连接属性map
	static Map<String,Map<String,String>> dsMap = new HashMap<>();
	//数据源dblist
	static List<String> dsList = new ArrayList<>();
	
	//加载属性
	static{
		System.out.println(DateUtils.getDateTimeNow() + " load properties ===================================");
		try {
			InputStream in = new FileInputStream(appFilePath);
			p.load(in);
			in.close();
			initJobList();
			initDsMap();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}	

	public static String getProperty(String name) {
		return p.getProperty(name);
	}

	public static void setProperty(String name, String value) {
		p.setProperty(name, value);
	}

	private static void initJobList(){
		Iterator it = p.keySet().iterator();
		while (it.hasNext()) {
			String key = (String) it.next();
			if (key.contains("model")) {
				String jobDepencyInfo =p.getProperty(key);
				String [] dep = jobDepencyInfo.split(",");
				jobDepMap.put(key, dep);
			}
		}
	}
	/**
	 * set数据源的map
	 */
	private static void initDsMap(){
		Iterator it = p.keySet().iterator();
		while (it.hasNext()) {
			String key = (String) it.next();
			if (key.contains("ds")) {
				String[] keySplit = key.split("\\.");
				String dsName = keySplit[1];
				if (dsMap.containsKey(dsName)) {
					Map<String,String> dsDetail = dsMap.get(dsName);
					dsDetail.put(keySplit[3], p.getProperty(key));
				} else {
					Map<String,String> dsDetail = new HashMap<>();
					dsDetail.put(keySplit[3], p.getProperty(key));
					dsMap.put(dsName, dsDetail);
					dsList.add(dsName);
				}
			}
		}
	}
	public static Map<String,String []> getJobDep(){
		return jobDepMap;
	}

	public static Map<String, Map<String, String>> getDsMap() {
		return dsMap;
	}

	public static List<String> getDsList() {
		return dsList;
	}
	
	
}
